package com.chenwenxuan.sync.console.dao;

import java.util.List;

import com.chenwenxuan.sync.console.model.Task;

import static com.chenwenxuan.sync.utils.Commons.*;

public class TaskDao extends AbstractDao {
	
	public int countTasks() {
		return getJdbcTemplate().queryForInt(countTasksSql);
	}
	
	public int countTaskByExecuteGroup(String executeGroup) {
		return getJdbcTemplate().queryForInt(countTasksByExecuteGroupSql, new Object[]{executeGroup});
	}
	
	@SuppressWarnings("unchecked")
	public List <Task> queryTasks(int start, int amount) {
		return (List <Task>) getJdbcTemplate().query(queryTasksSql, new Integer[]{start, amount}, new RowMappers.TaskRowMapper());
	}
	
	@SuppressWarnings("unchecked")
	public List <Task> queryTasksByExecuteGroup(int start, int amount, String executeGroup) {
		return (List <Task>) getJdbcTemplate().query(queryTasksqueryTasksByExecuteGroupSql, new Object[]{start, amount, executeGroup}, new RowMappers.TaskRowMapper());
	}
	
	@SuppressWarnings("unchecked")
	public List <Task> queryTasksByCatalog(int catalogId) {
		return (List <Task>) getJdbcTemplate().query(queryTasksByCatalogSql, new Object[]{catalogId}, new RowMappers.TaskRowMapper());
	}
	
	public Task queryTask(int taskId) {
		return (Task) getJdbcTemplate().queryForObject(queryTaskSql, new Object[]{taskId}, new RowMappers.TaskRowMapper());
	}
	
	public void updateTaskLastExecute(Task task) {
		getJdbcTemplate().update(updateTaskLastExecuteSql, new Object[]{ task.getLastExecute(), task.getTaskId()});
	}
	
	public void updateTaskCatalog(Task task) {
		getJdbcTemplate().update(updateTaskCatalogSql, new Object[]{ task.getCatalogId(), task.getTaskId()});
	}
	
	public void addTask(Task task) {
		getJdbcTemplate().update(addTaskSql, 
				new Object[] {
					task.getTaskId(), 
					task.getTaskName(), 
					task.getTaskType(), 
					task.getFreq(), 
					task.getStartTime(), 
					task.getLastExecute(),
					boolToInt(task.isDebugEnable()), 
					task.getCronExpression(), 
					task.getScheduleType(), 
					boolToInt(task.isTestEnable()),
					task.getExecuteOrder(), 
					task.getMaxRetry(), 
					task.getRetryType(), 
					task.getCondType(), 
					task.getCondText(),
					task.getExecuteGroup(),
					task.getCatalogId()
				});
	}
	
	public void deleteTask(Task task){
		getJdbcTemplate().update(deleteTaskSql, new Object[]{task.getTaskId()});
	}
	
	public void updateTask(Task task) {
		deleteTask(task);
		addTask(task);
	}

	private static final String updateTaskLastExecuteSql = "update task t set t.last_execute = ? where t.task_id = ?";
	private static final String updateTaskCatalogSql = "update task t set t.catalog_id = ? where t.task_id = ?";
	
	private static final String countTasksByExecuteGroupSql = "select count(*) from t_task where \"ENABLE\" = 1 and execute_group = ?";
	private static final String countTasksSql = "select count(*) from t_task where \"ENABLE\" = 1";
	private static final String deleteTaskSql = "delete from task where t.task_id = ?";
	
	private static final String selectFieldsSql =  
			"t.task_id, "
			+ "t.task_name, "
			+ "t.task_type, "
			+ "t.freq, "
			+ "t.start_time, "
			+ "t.last_execute, "
			+ "t.debug_enable, "
			+ "t.CRONEXPRESSION, "
			+ "t.SCHEDULE_TYPE, "
			+ "t.TEST_ENABLE, "
			+ "t.EXECUTE_ORDER, "
			+ "t.MAX_RETRY, "
			+ "t.RETRY_TYPE, "
			+ "t.COND_TYPE, "
			+ "t.COND_TEXT, "
			+ "t.EXECUTE_GROUP, "
			+ "t.CATALOG_ID ";
	
	
	private static final String queryTasksByCatalogSql = 
				"SELECT " + selectFieldsSql + "FROM task t where t.CATALOG_ID = ? ";
	
	private static final String queryTasksSql = 
			   "SELECT limit ?, ? " + selectFieldsSql + "FROM (SELECT * FROM task where \"ENABLE\" = 1 ORDER BY task_id, execute_order ) t ";
	
	private static final String queryTasksqueryTasksByExecuteGroupSql = 
			   "SELECT limit ?, ? " + selectFieldsSql + "FROM (SELECT * FROM task where \"ENABLE\" = 1 and execute_group = ? ORDER BY task_id, execute_order ) t ";
	
	private static final String queryTaskSql = 
			   "SELECT limit ?, ? " + selectFieldsSql + "FROM task t where t.task_id = ? ";
	
	private static final String addTaskSql =
			"insert into task ("
					+ "task_id, "
					+ "task_name, "
					+ "task_type, "
					+ "freq, "
					+ "start_time, "
					+ "last_execute, "
					+ "debug_enable, "
					+ "CRONEXPRESSION, "
					+ "SCHEDULE_TYPE, "
					+ "TEST_ENABLE,"
					+ "EXECUTE_ORDER, "
					+ "MAX_RETRY, "
					+ "RETRY_TYPE, "
					+ "COND_TYPE, "
					+ "COND_TEXT,"
					+ "EXECUTE_GROUP,"
					+ "CATALOG_ID"
					+ ") "
			+ "values "
					+ "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
	
}
